package cn.yunhe.util;

import org.apache.ibatis.io.Resources;
import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;
import org.apache.ibatis.session.SqlSessionFactoryBuilder;

import java.io.IOException;
import java.io.Reader;

public class SqlSessionFactoryUtil2 {

    private static SqlSessionFactory factory;

    public SqlSessionFactoryUtil2() {
    }

    static{
        Reader reader=null;
        try{
            reader= Resources.getResourceAsReader("mybatis2.xml");
            factory=new SqlSessionFactoryBuilder().build(reader);
        }catch(IOException e){
            e.printStackTrace();
        }finally {
            if(reader!=null){
                try{
                    reader.close();
                }catch(IOException e){
                    e.printStackTrace();
                }
            }
        }
    }

    public SqlSession getSqlSession(){
        return factory.openSession();
    }
}
